package com.example.service;


import com.example.DTO.AfterSalesApplicationDTO;
import com.example.entity.AfterSalesApplication;
import com.example.entity.Product;
import com.example.mapper.AfterSalesApplicationMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AfterSalesApplicationService {
    @Resource
    private AfterSalesApplicationMapper afterSalesApplicationMapper;

    public Integer submitAfterSalesApplication(AfterSalesApplication application) {
        //根据订单ID查询商品对应的商铺
       Integer sId=afterSalesApplicationMapper.selectSidByOid(application.getOrderItemsId());
        //在售后表里面插入售后店铺的ID
        application.setStoreId(sId);
        int result = afterSalesApplicationMapper.insertAfterSalesApplication(application);
        if (result > 0) {
            return application.getApplicationId();
        }
        return null;
    }

    public AfterSalesApplication getApplicationStatus(Integer applyId) {
        return afterSalesApplicationMapper.getAfterSalesApplicationById(applyId);
    }

    public void replyToAfterSalesApplication(AfterSalesApplication application) {
        afterSalesApplicationMapper.updateAfterSalesApplicationReply(application);
    }

    public boolean handleAfterSalesApplication(AfterSalesApplication application) {
        int result = afterSalesApplicationMapper.handleAfterSalesApplication(application);
        return result > 0;
    }

    public PageInfo<AfterSalesApplication> getPendingAfterSalesApplications( Integer sId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<AfterSalesApplication> list = afterSalesApplicationMapper.getPendingAfterSalesApplications(sId);
        return PageInfo.of(list);
    }

    public AfterSalesApplicationDTO getApplyIdDetail(Integer applyId) {
          return afterSalesApplicationMapper.getApplyIdDetail(applyId);
    }

    public PageInfo<AfterSalesApplication> getHistoryAfterSalesApplications(Integer sId,Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<AfterSalesApplication> list = afterSalesApplicationMapper.getHistoryAfterSalesApplications(sId);
        return PageInfo.of(list);
    }
    //买家获取该商品的所有售后状态
    public List<AfterSalesApplicationDTO> getApplicationAllStatus(Integer orderItemsId) {
        List<AfterSalesApplicationDTO> list = afterSalesApplicationMapper.getApplicationAllStatus(orderItemsId);
        return list;
    }

}
